package team.stride.tabs.home;

import java.text.DecimalFormat;

import team.stride.R;
import team.stride.gmap.GePoint;
import team.stride.tabs.TabGroupActivity;
import team.stride.utils.UtilsStride;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

public class RunningActivity extends Activity
{
	private static DecimalFormat df = new DecimalFormat("0.###");

	private LocationManager lm;
	private LocationListener locationListener;

	// GUI
	private TextView distanceTV;
	private TextView speedTV;
	private Button stopTrackBtn;
	private Button resumeTrackBtn;
	private Chronometer chronos;

	public boolean recording = true;
	private boolean firstTime = true;

	private long totalDistance = 0;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.running_layout);
		speedTV = (TextView) findViewById(R.id.runningSpeedTV);
		distanceTV = (TextView) findViewById(R.id.runningDistTV);
		chronos = (Chronometer) findViewById(R.id.runningTimeTV);
		stopTrackBtn = (Button) findViewById(R.id.stop_track_btn);
		resumeTrackBtn = (Button) findViewById(R.id.resume_track_btn);

		totalDistance = UtilsStride.totalDistance;
		chronos.setBase(SystemClock.elapsedRealtime() - UtilsStride.totalTime);

		if (recording && firstTime)
		{
			chronos.setBase(SystemClock.elapsedRealtime());
			chronos.start();
			firstTime = false;
		}

		OnClickListener stopTrackBtnListener = new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// save data
				UtilsStride.addTrackToDB(totalDistance, SystemClock.elapsedRealtime() - chronos.getBase());

				Intent intent = new Intent(getParent(), FinalResultsActivity.class);
				TabGroupActivity parentActivity = (TabGroupActivity) getParent();
				parentActivity.startChildActivity("FinalResultsActivity", intent);
			}
		};
		stopTrackBtn.setOnClickListener(stopTrackBtnListener);

		OnClickListener resumeTrackBtnListener = new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (recording == true)
				{
					recording = false;
					chronos.stop();
				}
				else
				{
					recording = true;
					chronos.start();
				}
			}
		};
		resumeTrackBtn.setOnClickListener(resumeTrackBtnListener);

		// GPS
		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationListener = new MyLocationListeners();
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);
	}

	@Override
	public void onResume()
	{
		super.onResume();
		UtilsStride.pathPoints.clear();
		totalDistance = 0;
	}

	class MyLocationListeners implements LocationListener
	{

		private double lat = 0, lng = 0, oldLat = 0, oldLng = 0;
		private double mySpeed;

		@Override
		public void onLocationChanged(Location loc)
		{
			float[] results = new float[4];
			if (loc != null && recording)
			{
				if (!loc.hasSpeed())
				{
					mySpeed = loc.getSpeed();
					speedTV.setText("" + mySpeed + "m/s");
				}
				oldLat = lat;
				oldLng = lng;
				lat = loc.getLatitude();
				lng = loc.getLongitude();

				if (oldLat != 0 && oldLng != 0)
				{
					Location.distanceBetween(oldLat, oldLng, lat, lng, results);
					setKms(results[0]);
				}

				GePoint geop = new GePoint((int) (lat * UtilsStride.GEO_CONV), (int) (lng * UtilsStride.GEO_CONV));
				UtilsStride.pathPoints.add(geop);
			}
		}

		private void setKms(float diff)
		{
			totalDistance += (long) diff;
			distanceTV.setText(df.format((double) totalDistance / 1000) + " Km");
		}

		@Override
		public void onProviderDisabled(String provider)
		{
			recording = false;
		}

		@Override
		public void onProviderEnabled(String provider)
		{
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras)
		{
		}
	}

}
